import settings
from common.log import logger
import traceback
import json
import requests
from bot.insurance_planner_gpt.qw_server.common import wework_constants


search_user_by_customer_id = 'http://8.142.23.75/workbench/case/getByCustomerId'

if settings.ONLINE:
    search_user_by_customer_id = 'http://172.26.5.106/workbench/case/getByCustomerId'

wework_config_list = [
    {"COMPANY_ID": "2243",
     "VERIFY_MSG": "您好，我是您的私人助理，希望和您多多交流，任何问题都可以随时和我沟通哈～",
     "WE_WORK_APP_KEY": "co1a6d3d26a7b947b9",
     "WE_WORK_APP_SECRET": "fzxDYDGVZW1DGY1mOIDzpUCNXlBWGyrtHlpdeGM7UMDpmVZXPCER",
     "WE_WORK_CALLBACK_TOKEN": "zjJVJTIbdlwVMmmgmmjdrijjp4l6fZqS",
     "WE_WORK_ENCODING_AES_KEY": "I6cSqfRDA7v8a4hGxbHipHZJ0HI353rx"
     }
]
if settings.ONLINE:
    wework_config_list = [{"COMPANY_ID": "2242",
                           "VERIFY_MSG": "您好，我是您的专属1对1的服务顾问，任何保险相关问题欢迎随时和我沟通～",
                           "WE_WORK_APP_KEY": "co6975f0027e9444fb",
                           "WE_WORK_APP_SECRET": "tivyA0MlOARoDyJUFN2UeSWEFLkzfVPCbpBtoyVhePbwzUBn42lG",
                           "WE_WORK_CALLBACK_TOKEN": "ekS3fV6UR6ZJ8DYmKKEFqPY9QP5FxGOO",
                           "WE_WORK_ENCODING_AES_KEY": "iUEjcF7FgZbHQXbyR1ng5A4P5yYBGw0Q"
                           },
                          {"COMPANY_ID": "2243",
                           "VERIFY_MSG": "您好，我是您的专属1对1的服务顾问，任何问题欢迎随时和我沟通哈～",
                           "WE_WORK_APP_KEY": "co1a6d3d26a7b947b9",
                           "WE_WORK_APP_SECRET": "fzxDYDGVZW1DGY1mOIDzpUCNXlBWGyrtHlpdeGM7UMDpmVZXPCER",
                           "WE_WORK_CALLBACK_TOKEN": "zjJVJTIbdlwVMmmgmmjdrijjp4l6fZqS",
                           "WE_WORK_ENCODING_AES_KEY": "I6cSqfRDA7v8a4hGxbHipHZJ0HI353rx"
                           },
                          {"COMPANY_ID": "2252",
                           "VERIFY_MSG": "企业主用款规划师，专业咨询评估。",
                           "WE_WORK_APP_KEY": "co402fefcf8e314c51",
                           "WE_WORK_APP_SECRET": "cWbH0yHQ8c2fGBSbIhjRms1RQ04iAuwkUtwJCEtc9b7Lah3Jzhlj",
                           "WE_WORK_CALLBACK_TOKEN": "dudGe0NMo0OwdCLPGBUkvrC8CuceU3nq",
                           "WE_WORK_ENCODING_AES_KEY": "wGgiuvSEj8RCIihzNO2vmEbdj7WiKKeC"
                           }
                          ]


def get_info_by_company_id(wework_config_list, target_company_id):
    for company in wework_config_list:
        if company["COMPANY_ID"] == target_company_id:
            return {
                "WE_WORK_APP_KEY": company["WE_WORK_APP_KEY"],
                "VERIFY_MSG": company["VERIFY_MSG"],
                "WE_WORK_APP_SECRET": company["WE_WORK_APP_SECRET"],
                "WE_WORK_CALLBACK_TOKEN": company["WE_WORK_CALLBACK_TOKEN"],
                "WE_WORK_ENCODING_AES_KEY": company["WE_WORK_ENCODING_AES_KEY"]
            }
    return {}


def get_info_by_app_key(wework_config_list, target_app_key):
    for company in wework_config_list:
        if company["WE_WORK_APP_KEY"] == target_app_key:
            return {
                "COMPANY_ID": company["COMPANY_ID"],
                "VERIFY_MSG": company["VERIFY_MSG"],
                "WE_WORK_APP_SECRET": company["WE_WORK_APP_SECRET"],
                "WE_WORK_CALLBACK_TOKEN": company["WE_WORK_CALLBACK_TOKEN"],
                "WE_WORK_ENCODING_AES_KEY": company["WE_WORK_ENCODING_AES_KEY"]
            }
    return {}


def get_info_from_session_id(session_id):
    company_id = None
    case_id = None
    try:
        if session_id:
            session_infos = session_id.split('-')
            company_id = session_infos[1]
            case_id = session_infos[2]
        return company_id, case_id
    except Exception:
        logger.error("解析session_id异常")
        return company_id, case_id

def get_robot_name(redis_memory,company_id,robot_id):
    robotName = ''
    try:
        keys = wework_constants.QW_PLANNER_REDIS_KEY_PREFIX + "robot_info:" + str(company_id)
        robotInfo = eval(redis_memory.get(keys))
        if robotInfo:
            robotName = robotInfo.get(str(robot_id))
    except Exception as ee:
        pass

    return robotName

def search_user_by_customerId(companyId,customerId):
    try:
        searchUrl = search_user_by_customer_id + '?companyId={}&customerId={}'.format(companyId,customerId)
        logger.info("获取客户信息{}---{}".format(customerId,searchUrl))
        r = requests.get(searchUrl, timeout=2, proxies={"http": None, "https": None})
        userInfoVal = eval(r.text)
        if userInfoVal.get('code') in ['200',200]:
            return userInfoVal.get('data')
        return None
    except Exception as ee:
        logger.error(traceback.format_exc())
        return None
